package cn.tedu.project.oa.sys.user.controller;


import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.response.StatusCode;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import cn.tedu.project.oa.sys.user.pojo.dto.*;
import cn.tedu.project.oa.sys.user.pojo.vo.LoginVO;
import cn.tedu.project.oa.sys.user.pojo.vo.UserVO;
import cn.tedu.project.oa.sys.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "01.用户模块")
@Slf4j
@RequestMapping("/oa/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录功能
     *
     * @param loginPara
     * @return JsonResult
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public JsonResult login(@Valid @RequestBody LoginPara loginPara) {
        log.debug("loginPara: {}", loginPara);
        LoginVO login = userService.login(loginPara);
        return JsonResult.ok(StatusCode.LOGIN_SUCCESS, login);
    }

    /**
     * 添加用户功能
     *
     * @param addUserPara 用户信息
     * @return JsonResult
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
    public JsonResult addUser(@RequestBody  AddUserPara addUserPara) {
        log.debug("新增用户信息:addUserPara: {}", addUserPara);
        userService.addUser(addUserPara);
        return JsonResult.ok();
    }

    /**
     * 编辑用户账号状态
     *
     * @param id     用户id
     * @param status 账号状态
     * @return JsonResult
     */
    @ApiOperation(value = "编辑账号状态")
    @PostMapping("/editStatus/{id}/{status}")
    public JsonResult editStatus(@PathVariable Long id, @PathVariable int status) {
        log.debug("编辑用户账号状态:userId: {}", id);
        userService.editUserStatus(id, status);
        return JsonResult.ok();
    }

    /**
     * 编辑用户信息
     *
     * @param userEditPara
     * @return JsonResult
     */
    @ApiOperation(value = "编辑用户信息")
    @PostMapping("/edit")
    public JsonResult editUser(@Valid @RequestBody UserEditPara userEditPara) {
        log.debug("编辑用户信息:addUserPara: {}", userEditPara);
        userService.editUser(userEditPara);
        return JsonResult.ok();
    }

    /**
     * 查询用户信息
     *
     * @param userQueryPara
     * @return JsonResult
     */
    @ApiOperation(value = "查询用户信息")
    @PostMapping("/userlist")
    public JsonResult getUserList(@RequestBody UserQueryPara userQueryPara) {
        log.debug("userQueryPara: {}", userQueryPara);
        List<UserVO> userVOList = userService.queryUser(userQueryPara);
        return JsonResult.ok(userVOList);
    }

    @ApiOperation(value = "查询用户信息")
    @GetMapping("/detail/{id}")
    public JsonResult getUserById(@PathVariable Long id) {
        log.debug("根据id查询用户信息:id: {}", id);
        UserVO userVO = userService.queryUserById(id);
        return JsonResult.ok(userVO);
    }

    /**
     * 修改密码
     *
     * @param passwordAlterPara 密码修改参数
     * @return JsonResult
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/editPassword")
    public JsonResult editPassword(@Valid @RequestBody PasswordAlterPara passwordAlterPara) {
        log.debug("修改密码:passwordAlterPara: {}", passwordAlterPara);
        userService.editPassword(passwordAlterPara);
        return JsonResult.ok();
    }

    /**
     * 删除指定用户信息
     *
     * @param id 用户id
     * @return JsonResult
     */
    @ApiOperation(value = "删除用户信息")
    @PostMapping("/delete/{id}")
    public JsonResult deleteUser(@PathVariable Long id) {
        log.debug("删除用户信息:id: {}", id);
        userService.deleteUser(id);
        return JsonResult.ok();
    }

    /**
     * 退出登录
     */
    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public JsonResult logout() {
        log.debug("退出登录");
        return JsonResult.ok();
    }

    /**
     * 查询用户总数
     */
    @ApiOperation(value = "查询用户数")
    @GetMapping("count")
    public JsonResult countUser(){
        log.debug("查询用户数");

        int num = userService.count();

        return JsonResult.ok(num);
    }


    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "2";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
